﻿
/// <summary>
/// Summary description for UserScreenControl
/// 组织前端的UI上的数据
/// </summary>
/// 
namespace John
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.Security;

    public class UserScreenControl
    {
        public UserScreenControl()
        {
            //
            // TODO: Add constructor logic here
            //
        }

         public static bool CheckUser(string userName, string strPassword)//检查 管理员名和密码是否正确
        {
            bool bTemp = false;

            strPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(strPassword, "MD5");

            DBConn myDB = new DBConn();
            string mySql = "select hyname,pws from Muser where hyname='" + userName + "' and pws='" + strPassword + "'";
            SqlDataReader mydr = myDB.getDataReader(mySql);
            if (mydr.Read())
            {
                bTemp = true;
            }
            else
            {
                bTemp = false;
            }

            mydr.Close();
            myDB.Close();

            return bTemp;
        }


        public static string GetDataTime()
        {
            return DateTime.Now.ToLongDateString();
        }

        /// <summary>
        /// 获取最新上架产品
        /// </summary>
        /// <param name="aspName"></param>
        /// <param name="uName"></param>
        /// <returns></returns>
        public static DataSet GetNewDataList(string aspName, string uName = null)
        {
            DBConn myDB = null;
            DataSet dataSet = null;
            string querySql = String.Empty;
            try
            {
                myDB = new DBConn();
                switch (aspName)
                {
                    case "Default":
                        {
                            //Default页面获取热卖产品
                            querySql = "select top 9 * from Products order by PID desc";
                            break;
                        }
                    case "ProductDisplay":
                    case "Company_Sale":
                    case "qglist":
                    case "zslist":
                        {
                            querySql = "select * from Products where uname='" + uName + "' order by PID desc";
                            break;
                        }
                }
                dataSet = myDB.getDataSet(querySql);
                myDB.Close();
                return dataSet;
            }
            catch (Exception)
            {
                myDB.Close();
                throw new Exception("你所访问的页面出错了！请重试一遍。");
            }
        }

        /// <summary>
        /// 获取热卖、推荐产品
        /// </summary>
        /// <returns></returns>
        public static DataSet GetHotDataList(string aspName)
        {
            DBConn myDB=null;
            DataSet dataSet=null;
            try
            {
                myDB = new DBConn();
                string querySql = String.Empty;
                switch (aspName)
                {
                    case "Default":
                        {
                            querySql = "select top 6 PID,PPicture,substring(pname,0,8)+'...' as pname from Products where PCommend=1 order by PID desc";
                            break;
                        }
                    case "Producte":
                        {
                            querySql = "select top 6 * from Products where PCommend=1 order by PID desc";
                            break;
                        }
                }
                dataSet = myDB.getDataSet(querySql);
                myDB.Close();
                return dataSet;
            }
            catch (Exception)
            {
                myDB.Close();
                throw new Exception("你所访问的页面出错了！请重试一遍。");
            }
            finally
            {
                myDB.Close();
            }
        }

        /// <summary>
        /// 获取网站公告信息
        /// </summary>
        /// <returns></returns>
        public static string GetAnnounce()
        {
            DBConn myDB = null;
            string announce = String.Empty;
            try
            {
                myDB = new DBConn();
                string mySql = "select * from append where id='3'";
                SqlDataReader mydr = myDB.getDataReader(mySql);
                if (mydr.Read())
                {
                    announce = mydr["text"].ToString();
                }
                return announce;
            }
            catch (Exception)
            {
                myDB.Close();
                throw new Exception("你所访问的页面出错了！请重试一遍。");
            }
        }

        #region Extend文件夹

        //TODO ;先取“公告栏”的信息
        public static string GetAboutUs1()
        {
            DBConn myDB = null;
            string lblAboutUs1 = string.Empty;
            try
            {
                myDB = new DBConn();
                string mySql = "select * from append where id='3'";
                SqlDataReader mydr = myDB.getDataReader(mySql);
                if (mydr.Read())
                {
                    lblAboutUs1 = mydr["text"].ToString();
                }
                return lblAboutUs1;
  
            }
            catch (Exception)
            {
                myDB.Close();
                throw new Exception("你所访问的页面出错了！请重试一遍。");
            }
            finally
            {
                myDB.Close();
            }
        }
        #endregion


        #region  Company文件夹下的方法

        /// <summary>
        /// 单位介绍
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="gsjs"></param>
        /// <param name="imgName"></param>
        public static void JieShao(string userName,out string gsjs, out string imgName)
        {
            gsjs = String.Empty;
            imgName = string.Empty;
            DBConn dbCon = null;
            SqlDataReader dr = null;
            string queryString = string.Empty;
            try
            {
                dbCon = new DBConn();
                queryString = "select gsjs,cimg from muser where hyname='" +userName + "'";
                dr = dbCon.getDataReader(queryString);
                if (dr.Read())
                {
                    gsjs = dr["gsjs"].ToString();
                    imgName = dr["cimg"].ToString();
                }
                dr.Close();
                dbCon.Close();

            }
            catch (Exception)
            {
                gsjs = String.Empty;
                imgName = string.Empty;
                dr.Close();
                dbCon.Close();
                throw new Exception("你所访问的页面出错了！请重试一遍。");
            }
        }

        /// <summary>
        /// 产品供应
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="yj"></param>
        public static void GY(string userName,out string yj)
        {
            yj = string.Empty;
            DBConn dbCon = null;
            SqlDataReader dr = null;
            string queryString = string.Empty;
            try
            {
                dbCon = new DBConn();
                queryString = "select count(*) as yj from Products where uname='" + userName + "'";
                dr = dbCon.getDataReader(queryString);
                if (dr.Read())
                {
                    yj = dr["yj"].ToString();
                }
                dr.Close();
                dbCon.Close();

            }
            catch (Exception)
            {
                yj = String.Empty;
                dr.Close();
                dbCon.Close();
                throw new Exception("你所访问的页面出错了！请重试一遍。");
            }
        }

        /// <summary>
        /// 产品求购
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="yj"></param>
        public static void QG(string userName, out string yj)
        {
            yj = string.Empty;
            DBConn dbCon = null;
            SqlDataReader dr = null;
            string queryString = string.Empty;
            try
            {
                dbCon = new DBConn();
                queryString = "select count(*) as yj from qqproduct where uname='" +userName + "'";
                dr = dbCon.getDataReader(queryString);
                if (dr.Read())
                {
                    yj = dr["yj"].ToString();
                }
                dr.Close();
                dbCon.Close();

            }
            catch (Exception)
            {
                yj = String.Empty;
                dr.Close();
                dbCon.Close();
                throw new Exception("你所访问的页面出错了！请重试一遍。");
            }
        }

        /// <summary>
        /// 招商
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="yj"></param>
        public static void ZS(string userName, out string yj)
        {
            yj = string.Empty;
            DBConn dbCon = null;
            SqlDataReader dr = null;
            string queryString = string.Empty;
            try
            {
                dbCon = new DBConn();
                queryString = "select count(*) as yj from zhaoshang where hyname='" +userName+ "'";
                dr = dbCon.getDataReader(queryString);
                if (dr.Read())
                {
                    yj = dr["yj"].ToString();
                }
                dr.Close();
                dbCon.Close();

            }
            catch (Exception)
            {
                yj = String.Empty;
                dr.Close();
                dbCon.Close();
                throw new Exception("你所访问的页面出错了！请重试一遍。");
            }
        }

        /// <summary>
        /// ??????? TODO jiangchen
        /// </summary>
        /// <param name="userName"></param>
        public static void UPphd(string userName)
        {
            DBConn dbCon = null;
            string queryString = string.Empty;
            try
            {
                dbCon = new DBConn();
                queryString = "update muser set phb=(select phb from muser where hyname='" +userName + "') +1 where hyname='" + userName + "'";
                dbCon.Update(queryString);
                dbCon.Close();

            }
            catch (Exception)
            {
                dbCon.Close();
                throw new Exception("你所访问的页面出错了！请重试一遍。");
            }
        }
        #endregion
    }
}